#!/usr/bin/env bash

export LANG=zh_CN.UTF-8
chack_ips(){
    FREE_IPS=()
    USED_IPS=()
    if [[ $# -eq 1 ]];then
         >./.ips
         IPs=${1}
         # 记录在用IP
         nmap -sP ${IPs}/24 | grep report | awk -F "[()]" '{print$2}' >> ./.ips
         IP3=${IPs%.*}
         for i in $(seq 1 254);do
             if ! grep -wq "${IP3}.${i}" ./.ips ;then
                FREE_IPS[${#FREE_IPS[@]}]=${IP3}.${i}
             else
                USED_IPS[${#USED_IPS[@]}]=${IP3}.${i}
             fi
         done
         export USED_IPS
         export FREE_IPS
         for u_ip in ${USED_IPS[@]};do  echo "[*]在线IP: ${u_ip}";done
         for f_ip in ${FREE_IPS[@]};do  echo "[ ]空闲IP: ${f_ip}";done
         echo -e "\n在线IP：${#USED_IPS[@]} 个 \n空闲IP：${#FREE_IPS[@]} 个"
     else
        echo "eg : sh $0 10.0.8.0"
    fi
}

chack_ips "$@"
